package com.sluggard.workflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sluggard.common.model.res.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricProcessInstanceQuery;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstanceQuery;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name="工作流-历史管理")
@RestController
@RequestMapping("workflow/history")
public class HistoryController {

    @Resource
    private org.camunda.bpm.engine.HistoryService historyService;

    @Operation(summary = "查询历史流程实例")
    @GetMapping("/process-instance")
    public Result<Page<HistoricProcessInstance>> listHistoricProcessInstances(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "processDefinitionKey", required = false) String processDefinitionKey) {
        
        HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
        if (StringUtils.isNotBlank(processDefinitionKey)) {
            query.processDefinitionKey(processDefinitionKey);
        }
        
        long total = query.count();
        List<HistoricProcessInstance> instances = query
                .orderByProcessInstanceStartTime().desc()
                .listPage((pageNo - 1) * pageSize, pageSize);

        Page<HistoricProcessInstance> page = new Page<>();
        page.setRecords(instances);
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        page.setTotal(total);
        
        return Result.ok(page);
    }

    @Operation(summary = "查询历史任务")
    @GetMapping("/task")
    public Result<Page<HistoricTaskInstance>> listHistoricTasks(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "processInstanceId", required = false) String processInstanceId) {
        
        HistoricTaskInstanceQuery query = historyService.createHistoricTaskInstanceQuery();
        if (StringUtils.isNotBlank(processInstanceId)) {
            query.processInstanceId(processInstanceId);
        }
        
        long total = query.count();
        List<HistoricTaskInstance> tasks = query
                .orderByHistoricTaskInstanceEndTime()
                .desc()
                .listPage((pageNo - 1) * pageSize, pageSize);

        Page<HistoricTaskInstance> page = new Page<>();
        page.setRecords(tasks);
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        page.setTotal(total);
        
        return Result.ok(page);
    }

    @Operation(summary = "查询流程活动")
    @GetMapping("/activity")
    public Result<List<HistoricActivityInstance>> listHistoricActivities(
            @RequestParam String processInstanceId) {
        List<HistoricActivityInstance> activities = historyService
                .createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId)
                .orderByHistoricActivityInstanceStartTime().asc()
                .list();
        return Result.ok(activities);
    }
} 